<?php
class RoomType extends AppModel {
	public $name         = 'RoomType';
    public $displayField = 'code';
    public $validate     = array(
        'code' => array(
            'between' => array(
                'rule'    => array('between', 4, 5),
                'message' => 'Room code must be 4 to 5 characters long'
            ),
            'isUnique' => array(
                'rule'    => 'isUnique',
                'message' => 'This code has already in use'
            )
        ),
        'description' => array(
            'minlength' => array(
                'rule'    => array('minLength', 3),
                'message' => 'Description must be at least 3 characters long'
            )
        ),
        'max_adult' => array(
            'rule'    => 'numeric',
            'message' => 'Maximum adults must be numeric.'
        ),
        'max_child' => array(
            'rule'    => 'numeric',
            'message' => 'Maximum children must be numeric.'
        ),
        'max_people' => array(
            'rule'    => 'numeric',
            'message' => 'Maximum people must be numeric.'
        ),
        'image' => array(
            'rule'       => array('extension', array('gif', 'jpeg', 'png', 'jpg')),
            'allowEmpty' => true,
            'message'    => 'Please supply a valid image.'
        )
    );
    public $belongsTo = array(
        'RoomGroup' => array(
            'className'    => 'RoomGroup',
            'fields'       => array('name'),
            'foreignKey'   => 'group'
        )
    );
    public $hasMany = array(
        'Room' => array(
            'className' => 'Room',
            'fields'    => array('id', 'hotel_id', 'number', 'description')
        )
    );

    public function afterSave () {
        //$this->clearCache('element__categories', 'views', '');
    } // afterSave

    public function afterDelete () {
        //$this->clearCache('element__categories', 'views', '');
    } // afterDelete

    public function availability ($roomTypeId, $date, $details=false) {
        if (is_array($date) && count($date) == 2) {
            list($d1, $m1, $y1) = split('[/.-]', $date[0]); // check-in date
            list($d2, $m2, $y2) = split('[/.-]', $date[1]); // check-out date

            $dates = array($y1.'-'.$m1.'-'.$d1); // check-in from specific date
        } else {
            $dates = array(date('Y-m-d')); // check-in date from today

            list($d1, $m1, $y1) = split('[/.-]', date('d-m-Y'));
            list($d2, $m2, $y2) = split('[/.-]', $date); // check-out date
        }

        $checkin  = strtotime($y1.'-'.$m1.'-'.$d1); // check-in time
        $checkout = strtotime($y2.'-'.$m2.'-'.$d2); // check-out time

        $loop = true;
        $pday = 1;

        while ($loop == true) {
            $ptime = ($pday * 86400) + $checkin;

            if ($ptime >= $checkout) {
                $loop = false;
                break;
            }

            $dates[] = date('Y-m-d', $ptime);

            $pday++;
        }

        if ($details) {
            $sql = 'SELECT r.`id`, r.`number`, r.`description`, r.`attributes`, t.`code`, t.`max_adult`, t.`max_child`, t.`max_people` FROM `rooms` AS r ' .
                   'INNER JOIN `room_types` AS t ' .
                   'ON r.`room_type_id`=t.`id` ' .
                   'WHERE r.`number` NOT IN ' .
                   ' (SELECT `room_number` FROM `stays` AS s WHERE s.`date` IN ("'.implode('","', $dates).'") AND s.`room_type_id`='.$roomTypeId.') ' .
                   'AND r.`room_type_id`='.$roomTypeId.' ORDER BY r.`number` ASC';
        } else {
            $sql = 'SELECT r.`id`, r.`number` FROM `rooms` AS r ' .
                   'WHERE r.`number` NOT IN ' .
                   ' (SELECT `room_number` FROM `stays` AS s WHERE s.`date` IN ("'.implode('","', $dates).'") AND s.`room_type_id`='.$roomTypeId.') ' .
                   'AND r.`room_type_id`='.$roomTypeId.' ORDER BY r.`number` ASC';
        }

        $data = $this->query($sql);
        $resp = array();

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $a) {
            if ($details == 2) {
                $resp[] = array(
                    'id'   => $a['r']['id'],
                    'cell' => array(
                        $a['r']['number'], $a['t']['code'], $a['r']['description'], $a['r']['attributes']
                    )
                );
            } else if ($details == 1) {
                $resp[] = array(
                    'id'       => $a['r']['id'],
                    'number'   => $a['r']['number'],
                    'type'     => $a['t']['code'],
                    'descr'    => $a['r']['description'],
                    'attr'     => $a['r']['attributes'],
                    'adults'   => $a['t']['max_adult'],
                    'child'    => $a['t']['max_child'],
                    'people'   => $a['t']['max_people'],
                    'rate'     => '',
                    'lastUsed' => '',
                    'actions'  => '<div class="selectRecord" onclick="selectRoom('.$a['r']['id'].', '.$a['r']['number'].');"></div>'
                );
            } else {
                $resp[] = array(
                    'id'     => $a['r']['id'],
                    'number' => $a['r']['number']
                );
            }
        }

        return $resp;
    } // availability
}  // RoomType
?>